package com.esd.device.utils;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class TcpResult {
    private String result;
    private int[] datas ;

    public static TcpResult error(){
        TcpResult tcpResult = new TcpResult();
        tcpResult.setResult("error");
        return tcpResult;
    }

    public static TcpResult miss(){
        TcpResult tcpResult = new TcpResult();
        tcpResult.setResult("");
        return tcpResult;
    }

    public static TcpResult success(int[] datas){
        TcpResult tcpResult = new TcpResult();
        tcpResult.setResult("success");
        tcpResult.setDatas(datas);
        return tcpResult;
    }

    public boolean isSuccess(){
        return result.equals("success");
    }

    public boolean isError(){
        return result.equals("error");
    }

    public boolean isMiss(){
        return result.isEmpty();
    }

    @Override
    public String toString() {
        if (this.isError()){
            return "gateway has shutdown!!";
        }else if (this.isMiss()){
            return "no return data, please check the terminal is running correctly?";
        }else {
            StringBuilder sb = new StringBuilder();
            sb.append("result: ").append(this.getResult()).append(", data:[");
            for (int i = 0; i < datas.length; i++) {
                sb.append("register ").append(i).append(" data: ").append(datas[i]);
                if (i < datas.length-1){
                    sb.append(", ") ;
                }
            }
            sb.append("]");
            return sb.toString();
        }
    }
}
